package com.nbstar.film.spider.phantomjs;

import java.io.*;

public class JSUtil {
    private static String projectPath = System.getProperty("user.dir") + "/filmspider";
    private static String jsPath = projectPath + File.separator + "huicong.js";
    private static String exePath = projectPath + File.separator + "phantomjs" + File.separator + "bin" + File.separator + "phantomjs.exe";


    public static void init() {
//        System.setProperty("phantomjs.binary.path", "/Users/shenjunlin/devTool/phantomjs/bin/phantomjs");
//
//        DesiredCapabilities desiredCapabilities = DesiredCapabilities.phantomjs();
//
//        WebDriver driver = new PhantomJSDriver(desiredCapabilities);


    }

    // 调用phantomjs程序，并传入js文件，并通过流拿回需要的数据。
    public static String getParseredHtml(String url) throws IOException
    {
        Runtime rt = Runtime.getRuntime();
        Process p = rt.exec(exePath + " " + jsPath + " " + url);
        InputStream is = p.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        StringBuffer sbf = new StringBuffer();
        String tmp = "";
        while ((tmp = br.readLine()) != null)
        {
            sbf.append(tmp);
        }
        String[] result = sbf.toString().split("companyServiceMod");
        String result2 = "";
        if(result.length >= 2)
        {
            result2 = result[1];
            if(result2.length() > 200)
            {
                result2 = result2.substring(0, 200);
            }
        }
        //System.out.println("resut2: "+result2);
        return result2;
    }

}
